package control;

import data.*;
import java.util.ArrayList;
import java.util.logging.Logger;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;

/**
 *
 * @author JtR
 */
public class GameEngine {
    private PlayerEntity player;
    private ArrayList<GameEntity> gameEntities;
    private Level currentLevel;

    public void destroy() {
        try {
            currentLevel.getLevelImage().destroy();
            player.getTextures().destroy();
        } catch (SlickException ex) {
            ex.printStackTrace();
        }
    }

    public GameEngine() {
        gameEntities = new ArrayList<GameEntity>();
        
        SpriteSheet playerTextures = null;
        try {
            playerTextures = new SpriteSheet("Crate.png", 256, 256);
            Image currentLevelTexture = new Image("level01.png");
            currentLevel = new Level(70, 180, 150, 150, currentLevelTexture);
        } catch (SlickException ex) {
            Logger.getLogger(GameEngine.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        player = new PlayerEntity("Benni", 50, 50, 10, 10, 200, playerTextures);

        resetCurrentLevel();
    }

    public void resetCurrentLevel() {
        // Reset player's position
        player.setX(currentLevel.getStartX());
        player.setY(currentLevel.getStartY());
        
        // Reset enemies' positions
        EnemyEntity e;
        for(GameEntity g : gameEntities) {

            if(g instanceof EnemyEntity) {
                e = (EnemyEntity)g;
                e.setX(e.getStartX());
                e.setY(e.getStartY());
            }
        }
    }

    public Level getCurrentLevel() {
        return currentLevel;
    }

    public ArrayList<GameEntity> getGameEntities() {
        return gameEntities;
    }

    public PlayerEntity getPlayer() {
        return player;
    }
}
